﻿@model GiftCardListModel

@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.GiftCards").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Gift cards");
}

@Html.AntiForgeryToken()
<div class="content-header clearfix">
    <h1 class="pull-left">
        @T("Admin.GiftCards")
    </h1>
    <div class="pull-right">
        <a href="@Url.Action("Create")" class="btn bg-blue">
            <i class="fa fa-plus-square"></i>
            @T("Admin.Common.AddNew")
        </a>
    </div>
</div>

<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.NopLabelFor(model => model.ActivatedId)
                                </div>
                                <div class="col-md-8">
                                    @Html.NopDropDownListFor(model => model.ActivatedId, Model.ActivatedList)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.NopLabelFor(model => model.CouponCode)
                                </div>
                                <div class="col-md-8">
                                    @Html.NopEditorFor(model => model.CouponCode)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.NopLabelFor(model => model.RecipientName)
                                </div>
                                <div class="col-md-8">
                                    @Html.NopEditorFor(model => model.RecipientName)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-8 col-md-offset-4">
                                    <button type="button" id="search-giftcards" class="btn btn-primary btn-search">
                                        <i class="fa fa-search"></i>
                                        @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-body">
                    <div id="giftcards-grid"></div>

                    <script>
                        $(document).ready(function () {
                            $("#giftcards-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("GiftCardList", "GiftCard"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: additionalData
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors"
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(defaultGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    pageSizes: [@(gridPageSizes)],
                                    @Html.Partial("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [{
                                    field: "AmountStr",
                                    title: "@T("Admin.GiftCards.Fields.Amount")"
                                }, {
                                    field: "RemainingAmountStr",
                                    title: "@T("Admin.GiftCards.Fields.RemainingAmount")"
                                }, {
                                    field: "GiftCardCouponCode",
                                    title: "@T("Admin.GiftCards.Fields.GiftCardCouponCode")"
                                }, {
                                    field: "RecipientName",
                                    title: "@T("Admin.GiftCards.Fields.RecipientName")"
                                }, {
                                    field: "IsGiftCardActivated",
                                    title: "@T("Admin.GiftCards.Fields.IsGiftCardActivated")",
                                    width: 200,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: '# if(IsGiftCardActivated) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                                }, {
                                    field: "CreatedOn",
                                    title: "@T("Admin.GiftCards.Fields.CreatedOn")",
                                    width: 200,
                                    type: "date",
                                    format: "{0:G}"
                                }, {
                                    field: "Id",
                                    title: "@T("Admin.Common.Edit")",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: '<a class="btn btn-default" href="Edit/#=Id#"><i class="fa fa-pencil"></i>@T("Admin.Common.Edit")</a>'                                
                                }]
                            });
                        });
                    </script>

                    <script type="text/javascript">
                        $(document).ready(function () {
                            $('#search-giftcards').click(function () {
                                var grid = $('#giftcards-grid').data('kendoGrid');
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                return false;
                            });
                        });

                        $("#@Html.FieldIdFor(model => model.CouponCode)").keydown(function (event) {
                            if (event.keyCode == 13) {
                                $("#search-giftcards").click();
                                return false;
                            }
                        });

                        function additionalData() {
                            var data = {
                                ActivatedId: $('#@Html.FieldIdFor(model => model.ActivatedId)').val(),
                                CouponCode: $('#@Html.FieldIdFor(model => model.CouponCode)').val(),
                                RecipientName: $('#@Html.FieldIdFor(model => model.RecipientName)').val()
                            };
                            addAntiForgeryToken(data);
                            return data;
                        }
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
